package cn.edu.swufe.surverpark.struts2.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.edu.swufe.surverpark.model.Right;
import cn.edu.swufe.surverpark.struts2.dao.BaseDao;
import cn.edu.swufe.surverpark.struts2.service.RightService;
import cn.edu.swufe.surverpark.util.DataUtil;
import cn.edu.swufe.surverpark.util.StringUtil;
import cn.edu.swufe.surverpark.util.ValidateUtil;

/**
 *@author 陈龙 E-mail:529914446@qq.com
 *@version创建时间:2015年5月6日上午2:32:40
 *类说明
 */
@Service("rightService")
public class RightServiceImpl extends BaseServiceImpl<Right> implements
		RightService {

	@Resource(name="rightDao")
	public void setDao(BaseDao<Right> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
	}

	@Override
	public void saveOrUpdateRight(Right r) {
		int pos = 0;
		long code = 1L;
		if(r.getId() == null){
//			String hql = "FROM Right r ORDER BY r.rightPos desc,r.rightCode desc";
//			List<Right> rights = this.findEntityByHQL(hql);
//			if(!ValidateUtil.isValid(rights)){
//				pos = 0;
//				code = 1L;
//			}else{
//				Right top = (Right) rights.get(0);
//				int topPos = top.getRightPos();
//				long topCode = top.getRightCode();
//				
//				if(topCode >= 1L << 60){
//					pos = topPos + 1;
//					code = 1;
//				}else{
//					pos = topPos;
//					code = topCode << 1;
//				}
//			}
			String hql = "select max(r.rightPos),max(r.rightCode) from Right r "
					+"WHERE r.rightPos = (SELECT MAX(rr.rightPos) from Right rr)";
			Object[] arr = (Object[]) this.uniqueResult(hql);
			Integer topPos = (Integer) arr[0];
			Long topCode = (Long) arr[1];
			if(topPos == null){
				pos = 0;
				code = 1L;
			}else{
				if(topCode >= (1l << 60)){
					pos = topPos + 1;
					code = 1L;
				}
				else{
					pos = topPos;
					code = topCode << 1;
				}
			}
			r.setRightPos(pos);
			r.setRightCode(code);
		}
		this.saveOrUpdateEntity(r);
	}

	@Override
	public void appendRightByURL(String url) {
		String hql = "select count(*) from Right r where r.rightUrl = ?";
		Long count = (Long) this.uniqueResult(hql,url);
		if(count == 0){
			Right r = new Right();
			r.setRightUrl(url);
			saveOrUpdateRight(r);
		}
	}

	@Override
	public void batchUpdateRights(List<Right> rights) {
		String hql = "update Right r set r.rightName = ?,r.common = ? where r.id = ?";
		if(ValidateUtil.isValid(rights)){
			for(Right r:rights){
				this.batchEntityByHQL(hql,r.getRightName(),r.isCommon(),r.getId());
			}
		}
	}

	@Override
	public List<Right> findRightsInRange(Integer[] ids) {
		if(ValidateUtil.isValid(ids)){
			String hql = "From Right r WHERE r.id in (" + StringUtil.arr2Str(ids) + ")";
			return this.findEntityByHQL(hql);
		}
		return null;
	}

	@Override
	public List<Right> findRightsNotInRange(Set<Right> rights) {
		if(!ValidateUtil.isValid(rights)){
			return this.findAllEntities();
		}else{
			String hql = "From Right r WHERE r.id not in (" + DataUtil.extractRightsIds(rights) + ")";
			return this.findEntityByHQL(hql);
		}
	}

	@Override
	public int getMaxRightPos() {
		String hql = "select max(r.rightPos) from Right r";
		Integer pos = (Integer) this.uniqueResult(hql);
		return pos == null? 0 :pos;
	}

}

